package com.ygj.shop.service.impl;

import com.base.common.persistent.dao.TNewsMapper;
import com.base.common.persistent.model.TNews;
import com.base.common.persistent.query.TNewsQuery;
import com.base.common.utils.StringUtils;
import com.ygj.shop.service.NewsService;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by asus on 2017-4-5.
 */
@Service("newsService")
public class NewsServiceImpl implements NewsService {

    @Resource
    private TNewsMapper tNewsMapper;

    @Override
    public int getNewsByConditionCount(TNewsQuery newsQuery) {
        return tNewsMapper.selectObjectByConditionCount(newsQuery);
    }

    @Override
    public List<TNews> getNewsByCondition(TNewsQuery newsQuery) {
        newsQuery.setOrderBy("c_order");
        List<TNews> tNewsList = tNewsMapper.selectObjectByCondition(newsQuery);
        List<TNews> newses = new ArrayList<>();
        //p.getContents().substring(p.getContents().indexOf("src") + 5, p.getContents().substring(p.getContents().indexOf("src") + 5).indexOf("\"") + (p.getContents().indexOf("src") + 5))
        tNewsList.stream().forEach((p) -> {
            p.setHeadImgUrl(p.getContents().indexOf("src") == -1 ? "" : ("http://120.25.120.99:2222" + p.getContents().substring(p.getContents().indexOf("src") + 5, p.getContents().substring(p.getContents().indexOf("src") + 5).indexOf("\"") + (p.getContents().indexOf("src") + 5))));
            /*String s = p.getContents().indexOf("src") == -1 ? "" : ("http://120.25.120.99:2222" + p.getContents().substring(p.getContents().indexOf("src") + 5, p.getContents().substring(p.getContents().indexOf("src") + 5).indexOf("\"") + (p.getContents().indexOf("src") + 5)));
            boolean b = true;
            while (b) {
                b = false;
            }*/
            Document document = Jsoup.parse("");
            Element head = document.head();
            head.append("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, minimum-scale=0.5, maximum-scale=2.0, user-scalable=false\" />");
            Element body = document.body();
            body.attr("style", "text-align: center;");
            body.append(p.getContents());
            Elements imgs = body.getElementsByTag("img");
            imgs.stream().forEach((item) -> {
                String src = item.attr("src");
                item.attr("src", "http://120.25.120.99:2222" + src);
            });
            p.setContents(document.toString());
            newses.add(p);
        });
        return tNewsList;
    }

    public static  void main(String[] args){
        String str = "1237567";
        System.out.println(str.indexOf("7"));
        System.out.println(str.substring(str.indexOf("7") + 1));
        System.out.println(str.substring(1, 3));
    }
}
